<?php

use app\model\PageSetting;
use think\migration\Migrator;
use app\model\Agent;

class AlterPageSettingChangeData extends Migrator
{
    public function up()
    {
        $pageSetting = new PageSetting();
        //先删除原来的数据
        PageSetting::destroy(function ($query) {
            $query->where('type', PageSetting::TYPE_COUPON)->where('sub_category', PageSetting::SUB_CATEGORY_SYJGQ)->where('agent_id','>', 0);
        });
        $agents = $this->agent();
        $varray_blocks = [
            [
                'name'      => '腾讯',
                'image_url' => env('app.api_url').'/static/images/home/icon_qqv.png',
                'brand_id'  => '2',
                'id'        => '2',
                'jump_type' => '5',
                'path'      => '/quanyi/detail?brandId=2',
            ],
            [
                'name'      => '优酷',
                'image_url' => env('app.api_url').'/static/images/home/icon_youku.png',
                'brand_id'  => '3',
                'id'        => '3',
                'jump_type' => '5',
                'path'      => '/quanyi/detail?brandId=3',
            ],
            [
                'name'      => '爱奇艺',
                'image_url' => env('app.api_url').'/static/images/home/icon_iqiyi.png',
                'brand_id'  => '1',
                'id'        => '1',
                'jump_type' => '5',
                'path'      => '/quanyi/detail?brandId=1',
            ],
            [
                'name'      => '喜马拉雅',
                'image_url' => env('app.api_url').'/static/images/home/icon_ximalaya.png',
                'brand_id'  => '5',
                'id'        => '5',
                'jump_type' => '5',
                'path'      => '/quanyi/detail?brandId=5',
            ],
            [
                'name'      => 'QQ音乐',
                'image_url' => env('app.api_url').'/static/images/home/icon_qqyinyue.png',
                'brand_id'  => '20',
                'id'        => '20',
                'jump_type' => '5',
                'path'      => '/quanyi/detail?brandId=20',
            ]
        ];

        if (!$agents->isEmpty()) {
            $agents->each(function (Agent $agent) use ($varray_blocks, $pageSetting){
                $king = array();
                //4.初始化金刚区
                $king['sub_category'] = PageSetting::SUB_CATEGORY_SYJGQ;
                $king['type']          = PageSetting::TYPE_COUPON;
                $king['source']        = PageSetting::SOURCE_ZDY;
                $king['subtitle_type'] = '1';
                foreach ($varray_blocks as $vajra_block) {
                    $king['setting_name']  = $vajra_block['name'];
                    $king['image']         = $vajra_block['image_url'];
                    $king['jump_type']     = $vajra_block['jump_type'];
                    $king['path']          = $vajra_block['path'];
                    $king['brand_id']      = $vajra_block['id'];
                    $king['agent_id']      = $agent->id;
                    $pageSetting->doSave($king);
                }
            });
        }
    }

    public function agent()
    {
        return Agent::query()->where('is_deleted', 0)->select();
    }
}
